package msc.ai.crypto.jade;

import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ShopperGui extends JFrame
{
	private ShopperAgent myShopperAgt;
	private JTextField txtCCNummber,txtCVVNummber,txtCCType,txtItemName,txtItemQty, txtMaxPrice;
	
	/**
	 * @param myShopper
	 * @throws HeadlessException
	 */
	/**
	 * @param myShopper
	 * @throws HeadlessException
	 */
	/**
	 * @param myShopper
	 * @throws HeadlessException
	 */
	public ShopperGui(ShopperAgent myShopper) throws HeadlessException
	{
		super(myShopper.getLocalName());
		this.myShopperAgt = myShopper;
		
		JPanel p = new JPanel();		
		p.setLayout(new GridLayout(6 , 2));
		
		p.add(new JLabel("Credit Card Number:"));
		txtCCNummber = new JTextField(15);
		p.add(txtCCNummber);
		
		p.add(new JLabel("Credit Card CVV:"));
		txtCVVNummber = new JTextField(15);
		p.add(txtCVVNummber);
		
		p.add(new JLabel("Credit Card Type:"));
		txtCCType = new JTextField(15);		
		p.add(txtCCType);
		
		p.add(new JLabel("Item Name:"));		
		txtItemName = new JTextField(15);
		p.add(txtItemName);
		
		p.add(new JLabel("Item Qty:"));
		txtItemQty = new JTextField(15);
		p.add(txtItemQty);
		
		p.add(new JLabel("Max Price:"));
		txtMaxPrice = new JTextField(15);
		p.add(txtMaxPrice);
		
		getContentPane().add(p, BorderLayout.CENTER);
		
		JButton addButton = new JButton("Buy");
		addButton.addActionListener( new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				try 
				{	
					String ccNo = txtCCNummber.getText().trim();
					String cvvNo = txtCVVNummber.getText().trim();
					String ccType = txtCCType.getText().trim();
					String itemName  = txtItemName.getText().trim();					
					String itemQtyStr = txtItemQty.getText().trim();
					String maxPriceStr = txtMaxPrice.getText().trim();
					int itemQty = 0;
					double maxPrice = 0.0d;
					
					if (!itemQtyStr.isEmpty()) itemQty = Integer.parseInt(itemQtyStr);
					if (!maxPriceStr.isEmpty()) maxPrice = Double.parseDouble(maxPriceStr);
					
					if (ccNo.isEmpty() || cvvNo.isEmpty() || ccType.isEmpty())
					{
						JOptionPane.showMessageDialog(ShopperGui.this, "Invalid credit card details entered. Please enter correct details", "Error", JOptionPane.ERROR_MESSAGE);
					}
					
					else if (itemName.isEmpty() || itemQty <= 0 || maxPrice < 0)
					{
						JOptionPane.showMessageDialog(ShopperGui.this, "Invalid item details entered. Please enter correct details", "Error", JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						BuyingItem bItem = new BuyingItem();
						bItem.setCreditCardNo(ccNo);
						bItem.setCreditCVVNo(cvvNo);
						bItem.setCreditCardType(ccType);						
						bItem.setItemSearching(itemName);
						bItem.setItemQtyRequired(itemQty);
						bItem.setMaxItemPrice(maxPrice);
						
						myShopperAgt.buyItem(bItem);
					}
					
					txtCCNummber.setText("");
					txtCVVNummber.setText("");
					txtCCType.setText("");
					txtItemName.setText("");
					txtItemQty.setText("");
					txtMaxPrice.setText("");
					
					
				}
				catch (Exception e) 
				{
					JOptionPane.showMessageDialog(ShopperGui.this, "Invalid values. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		} );
		p = new JPanel();
		p.add(addButton);
		getContentPane().add(p, BorderLayout.SOUTH);
		
		// Make the agent terminate when the user closes 
		// the GUI using the button on the upper right corner	
		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				myShopperAgt.doDelete();
			}
		} );
		
		setResizable(false);
		
	}
	
	public void showGui() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
		
	}
	
	public static void main(String[] args)
	{
		ShopperGui gui = new ShopperGui(new ShopperAgent());
		gui.showGui();
	}
	
	
	

}
